package com.example.speeddating.controller;

import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.AutheticInfo;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.entity.User;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.mapper.UserMapper;
import com.example.speeddating.service.AutheticInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@RestController
public class AutheticInfoController {

    @Autowired
    private AutheticInfoService autheticInfoService;

    @Autowired
    private UserMapper userMapper;

    @TokenCheck(roles = "user")
    @PostMapping("/user/authentication")
    public BaseResponse authentication(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("realName")==null||jsonString.get("cardId")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        AutheticInfo autheticInfo = new AutheticInfo();
        autheticInfo.setUserId(tokenEntity.getId());
        autheticInfo.setRealname(jsonString.get("realName").toString());
        autheticInfo.setCardId(jsonString.get("cardId").toString());
        autheticInfoService.autheticUser(autheticInfo);

//        更新
        User user = new User();
        user.setId(autheticInfo.getUserId());
        user.setIsAuthentic(true);
        userMapper.updateById(user);

        return RespGenerator.returnOK("认证成功");
    }

}
